package com.wuzuqing.android.eightdiagrams

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.*
import com.wuzuqing.android.eightdiagrams.model.CalculationTypeModel
import com.wuzuqing.android.eightdiagrams.utils.DataUtils
import com.wuzuqing.android.eightdiagrams.utils.EightDagramsManager
import com.wuzuqing.android.eightdiagrams.utils.LunarUtil


class WenWangShenShuActivity : BaseActivity() {

    lateinit var top: EditText
    lateinit var bottom: EditText
    lateinit var result: TextView
    lateinit var ganZhi: TextView
    lateinit var spinner: Spinner

    lateinit var eigContainer: View
    private var lists: ArrayList<EightDagramsView> = ArrayList()

    var model: CalculationTypeModel? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wen_wang)

        top = findViewById(R.id.top)
        bottom = findViewById(R.id.bottom)
        result = findViewById(R.id.result)
        spinner = findViewById(R.id.spinner)
        eigContainer = findViewById(R.id.eightDagramsContainer)

        ganZhi = findViewById(R.id.ganzhi)
        ganZhi.text = LunarUtil.getInstance().resetNow().toString()
        val mutableList = DataUtils.getTypeList()
        val adapter = ArrayAdapter<CalculationTypeModel>(this, android.R.layout.simple_spinner_item, android.R.id.text1, mutableList)
        spinner.adapter = adapter

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                model = mutableList!![position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
        model = mutableList!![0]
        lists?.add(findViewById(R.id.eightDagramsView1))
        lists?.add(findViewById(R.id.eightDagramsView2))
        lists?.add(findViewById(R.id.eightDagramsView3))
        lists?.add(findViewById(R.id.eightDagramsView4))
        lists?.add(findViewById(R.id.eightDagramsView5))
        findViewById<View>(R.id.btn).setOnClickListener {

            val topInt = top.text.toString()
            if (topInt.isEmpty()) {
                Toast.makeText(applicationContext, "请输入数字", Toast.LENGTH_LONG).show()
                return@setOnClickListener
            }
            val manager = EightDagramsManager()
            result.text = manager.getContent(topInt.toInt(), model?.number).toString()

            val imm = applicationContext.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(top.windowToken, 0)
        }


    }

}
